<?php

namespace app\api\model;

use app\common\model\Category as CategoryModel;

/**
 * 商品分类模型
 * Class Category
 * @package app\common\model
 */
class Category extends CategoryModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'wxapp_id',
//        'create_time',
        'update_time'
    ];

    public function getList($post) {
        $limit = isset($post['limits'])?$post['limits']:15;
        $parent_id = isset($post['parent_id'])?$post['parent_id']:0;

        if ($parent_id){
            return $this->with(['image'])
                ->where('parent_id', '=', $parent_id)
                ->where('status', '=', 0)
                ->order(['sort' => 'asc', 'create_time' => 'desc'])
                ->paginate($limit, false, [
                    'query' => \request()->request()
                ]);
        }else{
            return $this->with(['image'])
                ->where('parent_id', '=', $parent_id)
                ->where('status', '=', 0)
                ->where('image_id', '>', 0)
                ->order(['sort' => 'asc', 'create_time' => 'desc'])
                ->paginate($limit, false, [
                    'query' => \request()->request()
                ]);
        }


    }

    public function getChapter($post){
        $category_id = $post['parent_id'];
        //获取当前
        $chapterlist = $this->with(['image'])
            ->where('parent_id', '=', $category_id)
            ->where('status', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->select();
        $chapterlist_arr = $chapterlist->toArray();
        foreach ($chapterlist_arr as $k=>$v){
            if ($v['parent_id'] == 0){
                continue;
            }
            $nodesList = $this->with(['image'])
                ->where('parent_id', '=', $v['category_id'])
                ->where('status', '=', 0)
                ->order(['sort' => 'asc', 'create_time' => 'desc'])
                ->select();
            $chapterlist_arr[$k]['nodes'] = $nodesList->toArray();
        }
        return $chapterlist_arr;
    }

}
